package com.meilele.controller.guoziyang;


import com.meilele.biz.guoziyang.DwzBusiness;
import com.meilele.biz.guoziyang.SecDetailBusiness;
import com.meilele.biz.guoziyang.SecShowImageBusiness;
import com.meilele.entity.guoziyang.SecDetail;
import com.meilele.entity.guoziyang.SecShow;
import com.meilele.entity.zhengchao.AjaxMsgModel;
import com.meilele.util.guoziyang.Constants;
import com.meilele.util.guoziyang.Conta;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;


/*
此为装修效果图的后台管理Dwz
 */
@Controller
@RequestMapping("/background")
public class DwzController {

    @Resource
    private DwzBusiness dwzBusiness;
    @Resource
    private SecShowImageBusiness secShowImageBusiness;
    @Resource
    private SecDetailBusiness secDetailBusiness;

    @RequestMapping("/showDwz")
    public  String  show(){
        return "second/dwzshow";
    }

    @RequestMapping("/fenye")
    /*public String fenye(Model model, @RequestParam(value = "pageNum" ,defaultValue ="1") int pageNum){
    */public String fenye(Model model,int pageNum){
        int count = dwzBusiness.findCount();
        int page = (pageNum-1) * Conta.NUMBER_PAGINAL;
        List<SecDetail> secDetailList =  dwzBusiness.findLimint(page,Conta.NUMBER_PAGINAL);
        model.addAttribute("detailList" ,secDetailList);
        model.addAttribute("count" ,count);
        model.addAttribute("pageNum" ,pageNum);


     return "second/list";
    }




    @ResponseBody
    @RequestMapping("/add")
    public AjaxMsgModel add(@Param("lable") int[] laber, Model model, @RequestParam("file2") MultipartFile[] files, SecDetail detail) {
        try {
            String file0 = UUID.randomUUID().toString();
            File file = new File(Constants.IMAGE_PATH + file0);
            if (!file.exists()) {
                files[0].transferTo(file);
            }
            detail.setStyleDesigner(detail.getStyleCompany());
            detail.setStyleAlias(file0);
            detail.setStyleImage(files[0].getOriginalFilename());
            secDetailBusiness.saveDetail(detail);
            int id = detail.getStyleId();
            for (int i = 1; i < files.length; i++) {
                SecShow secShow = new SecShow();
                String file1 = UUID.randomUUID().toString();
                File fileImage = new File(Constants.IMAGE_PATH + file1);
                if (!fileImage.exists()) {
                    files[i].transferTo(fileImage);
                    secShow.setShowImage(file1);
                    secShow.setImageName(files[i].getOriginalFilename());
                    secShow.setImageDetail(id);
                    secShowImageBusiness.saveShow(secShow);
                }
            }
            for (int i = 0; i < laber.length; i++) {
                System.out.println(laber.length);
                int laberId = laber[i];
                int ww = secDetailBusiness.saveDeLa(laberId, id);
                System.out.println(ww);
            }

        } catch (Exception e) {


        }
        return new AjaxMsgModel("200","删除成功","sdogzy","","closeCurrent","");

    }
}
